<link rel="stylesheet" type="text/css" href="public/css/style.css" media="screen" />
<?php
require_once 'libraries/utilites/functions.php';
$path = "data/films.txt";
$full_path = realpath($path);
$films = mreadFile($path);
$INDEX = -1;

?>

<?php foreach ($films as $key => $item):?>
    <?php $item = trim($item);
    if (!empty($item)):?>
        <?php 
            $film = explode(";", $item);
            $img = trim($film[1]);
        ?>
        <?php if(intval($film[0])==intval($_GET['if'])):?>
        <?php $INDEX = $key;?>
        <div class="edit-film">
            <form method="POST">
            <img src="public/images/films/<?php echo $img ?>">
            <div class="edit-title">Title <input type="text" name="film_title" value="<?php echo $film[2] ?>"></div>
            <div class="edit-rating">Rating <input type="text" name="film_rating" value="<?php echo $film[3]?>"></div>
         
            <input type="submit" name="submit" value="Save">
            </form>
        </div>
        <?php endif;?>
    <?php endif;?>
<?php endforeach; ?>



<?php if (isset($_POST['submit'])): ?>
    <?php 
    $film = explode(';', $films[$INDEX]);        
    $film[2] = $_POST['film_title'];
    $film[3] = $_POST['film_rating'];
    $films[$INDEX] = implode(';', $film);
    mwriteFile($path, $films);
    header('Location: films.php');
    ?>
<?php endif; ?>

